package hot100;

public class Solution621 {
    public int leastInterval(char[] tasks, int n) {
        int max = 1;
        int[] count = new int[26];
        int num = 0;
        for (int i = 0; i < tasks.length; i++) {
            count[tasks[i] - 'A']++;
            if (count[tasks[i] - 'A'] == max) {
                num++;
            } else if (count[tasks[i] - 'A'] > max) {
                max = count[tasks[i] - 'A'];
                num = 1;
            }
        }
        return Math.max(tasks.length, (n + 1) * (max - 1) + num);
    }
}
